$(function(){
    $("#uploadBtn").click(function(){
        $.ajax({
            url:window.ctx +"/rest/user/checkRole",
            dataType:"json",
            type:"post",
            success:function(data){
                if(data){
                    $("#uploadForm").remove();
                    $.dialog({
                        title: "请选择要文件",
                        okVal: "上传",
                        cancelVal: "关闭",
                        content: $("#uploadTmp").html(),
                        lock: true,
                        ok: function () {
                            var fileName = $("#uploadForm").find(":file").val();
                            var regexp = /\.xls$/i;
                            if (!$.trim(fileName)) {
                                hintDialog("请选择xlsx格式文件并上传！");
                                return false;
                            }
                            if (!regexp.test(fileName)) {
                                hintDialog("文件类型不正确，请核对后再导入！");
                                $("#uploadForm").find(":file").val('');
                                return false;
                            }
                            //开始上传
                            $.ajaxFileUpload({
                                url: window.ctx + '/rest/owner/upload',
                                type: 'post',
                                secureuri: false,
                                fileElementId: 'fileId',
                                dataType: 'text',
                                success: function (result, status) {
                                    console.log(result);
                                    console.log(status);
                                    result = eval("(" + result + ")");
                                    var flag = result.id;
                                    if (flag == 1) {
                                        var msgArr = [];
                                        msgArr.push("导入完成,此次导入学生信息共" + result.total + "条，成功" + result.num + "条" );
                                        if(result.num != result.total ){
                                            msgArr.push("此次导入出现错误：" + result.updateError);
                                        }
                                        $.dialog.alert(msgArr.join("<br/>"), function () {
                                            location.href = window.ctx + "/owner/list";
                                        });
                                    } else {
                                        hintDialog(result.desc);
                                        $("#uploadForm").find(":file").val('');
                                    }
                                },
                                error: function () {
                                    hintDialog("导入过程出错！");
                                }
                            });
                            return false;
                        },
                        cancel: function () {
                            return true;
                        }
                    });
                }else {
                    art.dialog.tips("只有超级管理员可以上传文件");
                }

            }
        })
    });
    $("#downloadBtn").click(function(){
        location.href = window.ctx +"/rest/owner/download";
    });
})